﻿<Page
    x:Name="pageRoot"
    x:Class="CharacterMap.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:CharacterMap"
    xmlns:Interactivity="using:Microsoft.Xaml.Interactivity" 
    xmlns:Core="using:Microsoft.Xaml.Interactions.Core"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Page.Resources>
        <x:String x:Key="ChevronGlyph">&#xE26B;</x:String>
        <CollectionViewSource x:Name="itemsViewSource" Source="{Binding SelectedRanges}" IsSourceGrouped="True" ItemsPath="Characters" />
    </Page.Resources>

    <Grid x:Name="LayoutRoot" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="SelectionStates">
                <VisualState x:Name="None">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="characterPanel">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="Collapsed"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.ColumnSpan)" Storyboard.TargetName="semanticZoom">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0.25" Value="2"/>
                        </ObjectAnimationUsingKeyFrames>
                        <DoubleAnimation To="350" Duration="0:0:0.25" Storyboard.TargetProperty="X" Storyboard.TargetName="FlyoutTransform">
                            <DoubleAnimation.EasingFunction>
                                <CubicEase EasingMode="EaseIn" />
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Selected">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="characterPanel">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(Grid.ColumnSpan)" Storyboard.TargetName="semanticZoom">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="1"/>
                        </ObjectAnimationUsingKeyFrames>
                        <DoubleAnimation To="0" Duration="0:0:0.25" Storyboard.TargetProperty="X" Storyboard.TargetName="FlyoutTransform">
                            <DoubleAnimation.EasingFunction>
                                <CubicEase EasingMode="EaseOut"/>
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
            <VisualStateGroup x:Name="BusyStates">
                <VisualState x:Name="Ready"/>
                <VisualState x:Name="Busy">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" Storyboard.TargetName="characterView">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="False"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" Storyboard.TargetName="semanticZoom">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="False"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" Storyboard.TargetName="buttonFilter">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="False"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsEnabled" Storyboard.TargetName="buttonCopy">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="False"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="busyPanel">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="IsActive" Storyboard.TargetName="progressRing">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="True"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="350"/>
        </Grid.ColumnDefinitions>

        <SemanticZoom x:Name="semanticZoom" Grid.RowSpan="2" Grid.ColumnSpan="2">

            <SemanticZoom.ZoomedOutView>
                <GridView x:Name="gridViewZoomedOut"
                    AutomationProperties.AutomationId="ItemGridView"
                    AutomationProperties.Name="Grouped Items"
                    Padding="116,137,40,46"
                    SelectionMode="None"
                    ItemsSource="{Binding CollectionGroups, Source={StaticResource itemsViewSource}}">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Border Width="250" Height="270" DataContext="{Binding Group}" Background="#f6f6f6">
                                <Grid>
                                    <Border Background="#d6d6d6" Height="{Binding Characters.Count}" VerticalAlignment="Bottom"/>
                                    <TextBlock Foreground="#000000" Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource SubheaderTextBlockStyle}" Text="{Binding Name}" TextWrapping="Wrap" TextAlignment="Center"/>
                                    <TextBlock Foreground="#cc000000" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5">
                                        <Run Text="{Binding Characters.Count}"/>
                                        <Run Text=" characters"/>
                                    </TextBlock>
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView>
            </SemanticZoom.ZoomedOutView>

            <SemanticZoom.ZoomedInView>
                <GridView x:Name="gridView"
                    AutomationProperties.AutomationId="ItemGridView"
                    AutomationProperties.Name="Grouped Items"
                    Padding="116,137,40,46"
                    ItemsSource="{Binding Source={StaticResource itemsViewSource}}" 
                    SelectedItem="{Binding SelectedCharacter, Mode=TwoWay}">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Border Width="98" Height="98">
                                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding}" FontSize="48" FontFamily="{Binding DataContext.SelectedFont.Family, ElementName=gridView}"/>
                            </Border>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                    <GridView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <Grid Margin="0,0,0,2">
                                        <Button Foreground="{ThemeResource ApplicationHeaderForegroundThemeBrush}"
                                            AutomationProperties.Name="{Binding Name}"
                                            Click="Header_Click"
                                            Style="{StaticResource TextBlockButtonStyle}">
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Name}" Margin="0,-11,10,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap"/>
                                                <TextBlock Text="{StaticResource ChevronGlyph}" FontFamily="Segoe UI Symbol" Margin="0,-11,0,10" Style="{StaticResource SubheaderTextBlockStyle}" TextWrapping="NoWrap" />
                                            </StackPanel>
                                        </Button>
                                    </Grid>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                        </GroupStyle>
                    </GridView.GroupStyle>
                </GridView>
            </SemanticZoom.ZoomedInView>

        </SemanticZoom>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Margin="39,59,39,0" Command="{Binding NavigationHelper.GoBackCommand, ElementName=pageRoot}"
                        Style="{StaticResource NavigationBackButtonNormalStyle}"
                        VerticalAlignment="Top"
                        AutomationProperties.Name="Back"
                        AutomationProperties.AutomationId="BackButton"
                        AutomationProperties.ItemType="Navigation Button"/>
            <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}" Style="{StaticResource HeaderTextBlockStyle}" Grid.Column="1" 
                        IsHitTestVisible="false" TextWrapping="NoWrap" VerticalAlignment="Bottom" Margin="0,0,30,40"/>

            <StackPanel Orientation="Horizontal" Grid.Column="2" VerticalAlignment="Top">
                <AppBarButton x:Name="buttonFilter" Label="Filter" Icon="Filter">
                    <AppBarButton.Flyout>
                        <Flyout Opened="Flyout_Opened" Closed="Flyout_Closed">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="*"/>
                                </Grid.RowDefinitions>
                                <Button Content="Reset Defaults" Click="ButtonReset_Click"/>
                                <ListView x:Name="RangeSelector" Grid.Row="1" Width="420" Margin="5" SelectionMode="Multiple" SelectionChanged="RangeSelector_SelectionChanged">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Margin="5,10,5,0">
                                                <TextBlock.Inlines>
                                                    <Run Text="{Binding Name}"/>
                                                    <Run Text=" ("/>
                                                    <Run Text="{Binding Characters.Count}"/>
                                                    <Run Text=")"/>
                                                </TextBlock.Inlines>
                                            </TextBlock>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </Grid>
                        </Flyout>
                    </AppBarButton.Flyout>
                </AppBarButton>
                <AppBarButton x:Name="buttonCopy" Label="Copy" Icon="Copy" Command="{Binding CopyCommand}"/>
            </StackPanel>
        </Grid>

        <Grid Grid.Column="1" Grid.RowSpan="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Margin="0,15,0,15">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Text="Font: " Margin="10,0,0,0" FontSize="22" VerticalAlignment="Center"/>
                <ComboBox Grid.Column="1" Margin="10,0,10,0" VerticalAlignment="Center" ItemsSource="{Binding AllFonts}" FontSize="22" DisplayMemberPath="Name" SelectedItem="{Binding SelectedFont, Mode=TwoWay}"/>
            </Grid>
            <ScrollViewer Grid.Row="1" Margin="0,0,0,0" Visibility="Collapsed" x:Name="characterPanel" VerticalScrollMode="Auto" VerticalScrollBarVisibility="Auto" HorizontalScrollMode="Disabled" Grid.RowSpan="2" Grid.Column="1">
                <Grid>
                    <Border BorderBrush="Gray" BorderThickness="2,2,0,0" Opacity=".25">
                        <Border.Background>
                            <SolidColorBrush Color="#d6dee9"/>
                        </Border.Background>
                    </Border>
                    <local:CharacterView x:Name="characterView" Margin="15,15,15,5"/>
                </Grid>
                <ScrollViewer.RenderTransform>
                    <TranslateTransform x:Name="FlyoutTransform" X="350"/>
                </ScrollViewer.RenderTransform>
            </ScrollViewer>
        </Grid>

        <Grid x:Name="busyPanel" Grid.ColumnSpan="2" Grid.RowSpan="2" Visibility="Collapsed">
            <Border Background="{ThemeResource AppBarBackgroundThemeBrush}" Opacity=".25"/>
            <ProgressRing x:Name="progressRing" IsActive="False" Width="150" Height="150"/>
        </Grid>
    </Grid>
</Page>
